<template>
  <div>
    <div style="text-align:right;line-height:30px">
      <el-row :gutter="20">
        <el-col :span="16" style="text-align: left">
          <el-button class="tool_button" @click="clickNewButton" type="primary" size="mini" icon="el-icon-plus" plain v-show="permissions.indexOf('fmRoleAdd')>0" >新建角色</el-button>
          <el-button class="tool_button" @click="clickDeleteButton" type="danger" size="mini" icon="el-icon-delete" plain v-show="permissions.indexOf('fmRoleDelete')>0" >删除角色</el-button>
          <el-button class="tool_button" @click="clickRefreshButton" type="info" size="mini" icon="el-icon-refresh" plain>刷新</el-button>
        </el-col>

      </el-row>
      <div style="height: 10px"></div>
      <el-row :gutter="20">
        <el-col :span="5" style="text-align: left">
          <el-input v-model="inputText" placeholder="请输入角色名" size="small" clearable></el-input>
        </el-col>
        <el-col :span="5" style="text-align: left">
          <el-button type="primary" size="mini" icon="el-icon-search" plain @click="clickSearchButton"  >搜索</el-button>
        </el-col>

      </el-row>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData.datas"
      tooltip-effect="dark"
      :stripe="true"
      :border="true"
      size="mini"
      style="width: 100%;margin-top:20px"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        type="index"
        :index="indexMethod"
        label="序号"
        width="80">
      </el-table-column>
      <el-table-column
        prop="roleName"
        label="角色名"
        width="200">
      </el-table-column>
      <el-table-column
        prop="description"
        label="描述"
        width="220">
      </el-table-column>
      <el-table-column
        prop="createUser"
        label="创建人"
        width="130">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column label="操作" style="margin-right:10px;">
        <template slot-scope="scope">
          <i @click="showRolePersons(scope.row)" v-show="permissions.indexOf('fmRoleToUser')>0" ><icon name="people_list" :scale="2" class="pointcss" ></icon>&nbsp;</i>
          <i @click="showRoleAddUser(scope.row)"  v-show="permissions.indexOf('fmRoleUserAdd')>0"  > <icon name="addUser" :scale="2" class="pointcss" ></icon>&nbsp;</i>
          <icon name="edit" :scale="2" @click.native="handleUpdate(scope.row)" class="pointcss" v-show="permissions.indexOf('fmRoleUpdate')>0" ></icon>&nbsp;
          <i @click="goToPermissions(scope.row)" v-show="permissions.indexOf('fmRolePermissionsAdd')>0" > <icon name="exit" :scale="2" class="pointcss" ></icon>&nbsp;</i>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:10px;text-align: center">
      <el-pagination background layout="total,prev, pager, next,jumper" :total="tableData.totals" :current-page="tableData.currentPage" v-on:current-change="pageChange">
      </el-pagination>
    </div>
  </div>
</template>

<script type="text/javascript">
  import { getRoleList, getRoleListRemote, deleteRoleRemote } from '@/view/api/roleManager.js'
  import { yfuErrorToast, yfuSuccessToast } from '@/utils/yfuMessage.js'
  import { yfuPost } from '@/utils/request.js'
  import global_ from '@/utils/global.vue'
  import store from '@/store'
  import getPermissionByMenuId from '@/utils/MenuPermissionUtils.js'

  export default {
    props: ['menuId'],
    data () {
      return {
        permissions: '',
        /* table begin */
        tableData: {
          datas: [],
          currentPage: 1,
          totals: 0
        },
        multipleSelection: [],
        /* table end */

        /* search begin */
        select: '1',
        inputText: '',
        /* search end */

        /* temp begin */
        temp: {
          id: '',
          roleName: '',
          roleDescribe: '',
          createUser: '',
          createTime: ''
        }
      }
    },
    created: function () {
      let cThis = this

      getPermissionByMenuId(this.sessiongetJSON('user').id, this.menuId, function (permissionString) {
        cThis.permissions = permissionString
      })
    },
    mounted: function () {
      this.getAllDatas({currentPage: 1, pageSize: 10})
    },
    methods: {
      indexMethod (index) {
        index += 1
        let cp = this.tableData.currentPage - 1
        let indexValue = index + cp * 10
        return indexValue
      },
      toggleSelection (rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row)
          })
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      },
      handleSelectionChange (val) {
        this.multipleSelection = val
      },
      // 获取选中行
      getSelectedRows () {
        return this.multipleSelection
      },
      getAllDatas: function (param1) {
        let cThis = this
        if (global_.isLocalServer) {
          getRoleList({},
            function (data) {
              console.log(data)
              cThis.tableData = data
            },
            function (error) {
              console.log(error)
            }
          )
        } else {
          yfuPost(process.env.BASE_API, getRoleListRemote.url, param1, function (data) {
            cThis.tableData.totals = data.totals
            cThis.tableData.currentPage = data.currentPage
            cThis.tableData.datas = data.datas
          },
            function (error) {
              console.log(error)
            })
        }
      },
      pageChange (toPage) {
        if (toPage < 1) {
          toPage = 1
        }
        let param1 = this.createParam()
        param1.currentPage = toPage
        param1.pageSize = 10
        this.getAllDatas(param1)
      },
      // 新建
      clickNewButton () {
        this.$emit('listenToChildNewButtonEvent')
      },
      // 编辑
      handleUpdate (row) {
        this.temp = Object.assign({}, row) // copy obj
        this.$emit('listenToChildEditButtonEvent', Object.assign({}, row))
      },
      // 删除
      clickDeleteButton () {
        let cThis = this
        // 获取选中行的数据
        if (this.multipleSelection.length === 0) {
          yfuErrorToast('请至少选中一行')
        } else {
          this.$confirm('是否确认删除角色？', '删除角色', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if (!global_.isLocalServer) {
              console.log(cThis.multipleSelection)
              let param1 = {}
              let roleIds = ''
              for (let selection of cThis.multipleSelection) {
                roleIds += selection.id + ','
              }
              param1.roleIds = roleIds
              yfuPost(process.env.BASE_API, deleteRoleRemote.url, param1, function () {
                yfuSuccessToast('删除成功')
                cThis.refreshTable()
              })
            } else {
              yfuSuccessToast('删除成功')
            }
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
        }
      },
      showRolePersons (row) {
        let cThis = this
        console.log('rowid:' + row.id)
        store.commit('setRoleId', row.id)
        store.commit('setRoleName', row.name)
        cThis.$emit('roleToUser', row)
      },
      // 去添加用户列表
      showRoleAddUser (item) {
        this.$emit('showRoleAddUser', item)
      },
      refreshTable () {
        this.pageChange(1)
      },
      clickRefreshButton () {
        this.refreshTable()
      },
      // 点击搜索按钮
      clickSearchButton () {
        this.pageChange(1)
      },
      // 构建参数
      createParam (outerSearchParam) {
        let param1 = {}
        if (this.inputText == null || this.inputText === '') {
          return param1
        }
       // 角色名
        param1.roleName = this.inputText
        return param1
      },
      // 角色添加模块
      goToPermissions (row) {
        let cThis = this
        cThis.$emit('goToPermissions', row)
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  .el-select .el-input {
    width: 105px;
  }

</style>
